﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Reflection;
using System.Windows.Forms;

namespace CtrlSys
{
    public class ClassReg
    {
        public bool SetAutoRun(string keyName,string filePath)
        {
            bool result = false;
            try
            {
                RegistryKey rkSOFTWARE = Registry.CurrentUser.OpenSubKey("SOFTWARE");
                RegistryKey rkMicrosoft = rkSOFTWARE.OpenSubKey("Microsoft");
                RegistryKey rkWindows = rkMicrosoft.OpenSubKey("Windows");
                RegistryKey rkCurrentVersion = rkWindows.OpenSubKey("CurrentVersion");
                RegistryKey rkRun = rkCurrentVersion.OpenSubKey("Run", true);
                rkRun.SetValue(keyName, filePath);
                rkRun.Close();
                result = true;
            }
            catch (Exception ex)
            {
                CtrlLog.ClassLog Log = new CtrlLog.ClassLog(Application.ExecutablePath);
                Log.Append(MethodBase.GetCurrentMethod(), ex);
                MessageBox.Show(ex.Message);
            }
            return result;
        }

        public string GetServerIP(string ProjectName)
        {
            string result = string.Empty;
            try
            {
                RegistryKey rkCurrentUser = Registry.CurrentUser;
                RegistryKey rkSoftware = rkCurrentUser.OpenSubKey("Software");
                RegistryKey rkKengeeIT = rkSoftware.OpenSubKey(ProjectName);
                if (rkKengeeIT != null)
                {
                    result = rkKengeeIT.GetValue("ServerIP").ToString();
                }
            }
            catch (Exception ex)
            {
                CtrlLog.ClassLog Log = new CtrlLog.ClassLog(Application.ExecutablePath);
                Log.Append(MethodBase.GetCurrentMethod(), ex);
                MessageBox.Show(ex.Message);
            }
            return result;
        }

        public bool SetServerIP(string ProjectName, string ServerIP)
        {
            bool result = false;
            try
            {
                RegistryKey rkCurrentUser = Registry.CurrentUser;
                RegistryKey rkSoftware = rkCurrentUser.OpenSubKey("Software", true);
                RegistryKey rkKengeeIT = rkSoftware.CreateSubKey(ProjectName);
                rkKengeeIT.SetValue("ServerIP", ServerIP);
            }
            catch (Exception ex)
            {
                CtrlLog.ClassLog Log = new CtrlLog.ClassLog(Application.ExecutablePath);
                Log.Append(MethodBase.GetCurrentMethod(), ex);
                MessageBox.Show(ex.Message);
            }
            return result;
        }

        public bool SetLockNetwork(bool aEnable)
        {
            bool result = false;
            try
            {
                RegistryKey rkCurrentUser = Registry.CurrentUser;
                RegistryKey rkSoftware = rkCurrentUser.CreateSubKey("Software");
                RegistryKey rkPolicies = rkSoftware.CreateSubKey("Policies");
                RegistryKey rkMicrosoft = rkPolicies.CreateSubKey("Microsoft");
                RegistryKey rkWindows = rkMicrosoft.CreateSubKey("Windows");
                RegistryKey rkNC = rkWindows.CreateSubKey("Network Connections");
                rkNC.SetValue("NC_EnableAdminProhibits", "1", RegistryValueKind.DWord);

                string Value = "0";
                if (aEnable == false)
                {
                    Value = "1";
                }
                rkNC.SetValue("NC_LanConnect", Value, RegistryValueKind.DWord);//禁止停用网卡
                rkNC.SetValue("NC_AllowAdvancedTCPIPConfig", Value, RegistryValueKind.DWord);//禁止TCP协议高级选项
                rkNC.SetValue("NC_AddRemoveComponents", Value, RegistryValueKind.DWord);//禁止安装和卸载网络协议
                rkNC.SetValue("NC_ChangeBindState", Value, RegistryValueKind.DWord);//禁止启用和停止网络协议
                rkNC.SetValue("NC_LanChangeProperties", Value, RegistryValueKind.DWord);//禁止访问网络议属性
                rkNC.SetValue("NC_RenameConnection", Value, RegistryValueKind.DWord);//禁止重命名网卡

            }
            catch (Exception ex)
            {
                CtrlLog.ClassLog Log = new CtrlLog.ClassLog(Application.ExecutablePath);
                Log.Append(MethodBase.GetCurrentMethod(), ex);
                MessageBox.Show(ex.Message);
            }
            return result;
        }

        public string GetPcAnyWhereFileDir()
        {
            string result = string.Empty;
            try
            {
                RegistryKey rkLocalMachine = Registry.LocalMachine;
                RegistryKey rkSOFTWARE = rkLocalMachine.CreateSubKey("SOFTWARE");
                RegistryKey rkSymantec = rkSOFTWARE.CreateSubKey("Symantec");
                RegistryKey rkpcAnywhere = rkSymantec.CreateSubKey("pcAnywhere");
                RegistryKey rkCurrentVersion = rkpcAnywhere.CreateSubKey("CurrentVersion");
                RegistryKey rkSystem = rkCurrentVersion.CreateSubKey("System");
                result = rkSystem.GetValue("FilesDirectory").ToString();
            }
            catch (Exception ex)
            {
                CtrlLog.ClassLog Log = new CtrlLog.ClassLog(Application.ExecutablePath);
                Log.Append(MethodBase.GetCurrentMethod(), ex);
                MessageBox.Show(ex.Message);
            }
            return result;
        }

        //public bool ClearDNS()
        //{
        //    bool result = false;
        //    try
        //    {
        //        RegistryKey rkLocalMachine = Registry.LocalMachine;
        //        RegistryKey rkSystem = rkLocalMachine.CreateSubKey("System");
        //        RegistryKey rkControlSet001 = rkSystem.CreateSubKey("ControlSet001");
        //        RegistryKey rkServices = rkControlSet001.CreateSubKey("Services");
        //        RegistryKey rkTcpip = rkServices.CreateSubKey("Tcpip");
        //        RegistryKey rkParameters = rkTcpip.CreateSubKey("Parameters");
        //        RegistryKey rkInterfaces = rkParameters.CreateSubKey("Interfaces");
        //        string[] InterfaceSubKey=rkInterfaces.GetSubKeyNames();
        //        for (int i = 0; i < InterfaceSubKey.Length; i++)
        //        {
        //            RegistryKey rkInterfaceSubKey = rkParameters.CreateSubKey(InterfaceSubKey[i]);
        //            //object vNameServer = rkInterfaceSubKey.GetValue("NameServer");
        //            //if (vNameServer != null)
        //            //{
        //            //    rkInterfaceSubKey.DeleteValue("NameServer");
        //            //}
        //            rkInterfaceSubKey.SetValue("NameServer", string.Empty, RegistryValueKind.String);
        //            rkInterfaceSubKey.Close();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        CtrlLog.ClassLog Log = new CtrlLog.ClassLog(Application.ExecutablePath);
        //        Log.Append(MethodBase.GetCurrentMethod(), ex);
        //        MessageBox.Show(ex.Message);
        //    }
        //    return result;
        //}

        //private void SetRegistry_LockSetup(bool aEnable)
        //{
        //    //HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer

        //    RegistryKey rkLocalMachine = Registry.LocalMachine;
        //    RegistryKey rkSoftware = rkLocalMachine.CreateSubKey("Software");
        //    RegistryKey rkPolicies = rkSoftware.CreateSubKey("Policies");
        //    RegistryKey rkMicrosoft = rkPolicies.CreateSubKey("Microsoft");
        //    RegistryKey rkWindows = rkMicrosoft.CreateSubKey("Windows");
        //    RegistryKey rkInstaller = rkWindows.CreateSubKey("Installer");
        //    //          名称DisableUserInstalls 类型REG_DWord 值0x00000002(2) 
        //    //          名称DisableUserInstalls_Intelset_undo 类型REG_DWORD 值0x062ce6f0(103606000)

        //    rkInstaller.SetValue("DisableUserInstalls_Intelset_undo", "103606000", RegistryValueKind.DWord);

        //    string Value = "2";
        //    if (aEnable == false)
        //    {
        //        Value = "0";
        //    }
        //    rkInstaller.SetValue("DisableUserInstalls", Value, RegistryValueKind.DWord);
        //}
    }
}
